#ifndef MODELPREVIEW_H
#define MODELPREVIEW_H

#include <QOpenGLWidget>
#include <QOpenGLFunctions>
#include <QOpenGLShaderProgram>
#include <QOpenGLBuffer>
#include <QOpenGLTexture>
#include <QMouseEvent>
#include <QWheelEvent>
#include <QTimer>
#include <QTime>
#include <QVector>
#include <QMap>
#include <QString>
#include <assimp/scene.h>

// 骨骼结构体
struct Bone {
    QString name;
    QMatrix4x4 offsetMatrix;
    QMatrix4x4 finalTransformation;
    int parentIndex;
    QVector<int> children;
};

// 顶点权重结构体
struct VertexWeight {
    int boneIndex;
    float weight;
    unsigned int vertexIndex;
};

// 动画信息结构体
struct AnimationInfo {
    QString name;
    double duration;
    double ticksPerSecond;
    int channelCount;
    bool isPlaying;
};

class ModelPreview : public QOpenGLWidget, protected QOpenGLFunctions
{
    Q_OBJECT

public:
    explicit ModelPreview(QWidget *parent = nullptr);
    ~ModelPreview();

    void setModelScene(const aiScene *scene, const QString &modelPath);
    void clearModel();
    
    // 缩放控制
    void setScale(float scale);
    float getScale() const { return m_zoom; }
    void resetScale();
    void resetView();
    
    // 视图控制
    void setViewPreset(const QString &preset);
    void setRotationSensitivity(float sensitivity);
    void setAutoAdjustCamera(bool enabled);
    void adjustCameraForAnimation();
    
    // 预设视角
    void setFrontView();
    void setBackView();
    void setLeftView();
    void setRightView();
    void setTopView();
    void setBottomView();
    
    // 动画控制
    void setAnimationEnabled(bool enabled);
    QVector<AnimationInfo> getAnimationList() const;
    void playAnimation(int animationIndex);
    void stopAnimation();
    void pauseAnimation();
    void resumeAnimation();
    int getCurrentAnimationIndex() const;
    bool isAnimationPlaying() const;

signals:
    void scaleChanged(float scale);
    void animationListChanged();
    void animationStateChanged(int animationIndex, bool isPlaying);

protected:
    void initializeGL() override;
    void paintGL() override;
    void resizeGL(int w, int h) override;
    
    void mousePressEvent(QMouseEvent *event) override;
    void mouseMoveEvent(QMouseEvent *event) override;
    void wheelEvent(QWheelEvent *event) override;

private:
    void setupUI();
    void loadModelToBuffer(const aiScene *scene, const QString &modelPath);
    void loadAnimationList(const aiScene *scene);
    void loadBones(const aiScene *scene);
    void loadTextures(const aiScene *scene, const QString &modelDir);
    GLuint loadEmbeddedTexture(const aiTexture *texture);
    GLuint loadExternalTexture(const QString &filePath);
    void buildBoneHierarchy(const aiNode *node, int parentIndex);
    void updateAnimation(double timeInSeconds);
    void updateBoneTransforms(double timeInSeconds);
    void calculateBoneTransforms(const aiNode *node, const QMatrix4x4 &parentTransform);
    void applyAnimationTransforms();
    void playCurrentAnimation();
    QVector3D interpolatePosition(const aiNodeAnim* channel, double time);
    QQuaternion interpolateRotation(const aiNodeAnim* channel, double time);
    QVector3D interpolateScaling(const aiNodeAnim* channel, double time);
    
    // 新增：模型边界框和相机调整函数
    void calculateModelBounds();
    void adjustCameraToModel();
    void debugRenderState();
    void markViewSwitch();
    
    // 骨骼动画相关
    QVector<Bone> m_bones;
    QVector<QMatrix4x4> m_boneTransforms;
    QVector<VertexWeight> m_vertexWeights;
    QMap<QString, int> m_boneNameToIndex;
    
    // 动画控制
    QVector<AnimationInfo> m_animationList;
    int m_currentAnimationIndex;
    bool m_isAnimationPlaying;
    bool m_isAnimationPaused;
    double m_animationDuration;
    double m_ticksPerSecond;
    double m_animationTime;
    const aiAnimation* m_currentAnimation;
    
    // OpenGL相关
    QOpenGLShaderProgram *m_program;
    QOpenGLBuffer m_vertexBuffer;
    QOpenGLBuffer m_indexBuffer;
    QVector<GLuint> m_textures;
    
    // 着色器属性位置
    int m_posAttr;
    int m_colAttr;
    int m_texAttr;
    int m_normalAttr;
    int m_boneIndexAttr;
    int m_boneWeightAttr;
    
    // 着色器uniform位置
    int m_matrixUniform;
    int m_modelMatrixUniform;
    int m_hasTextureUniform;
    int m_boneTransformsUniform;
    
    // 模型数据
    const aiScene *m_scene;
    bool m_modelLoaded;
    QVector<float> m_vertices;
    QVector<unsigned int> m_indices;
    
    // 相机和变换
    QVector3D m_cameraPosition;
    QVector3D m_cameraTarget;
    QVector3D m_cameraUp;
    QMatrix4x4 m_projection;
    float m_zoom;
    QQuaternion m_rotation;
    
    // 鼠标控制
    QPoint m_lastMousePos;
    bool m_mousePressed;
    float m_rotationSensitivity;
    bool m_autoAdjustCamera;
    
    // 模型边界框
    QVector3D m_modelMin;
    QVector3D m_modelMax;
    QVector3D m_modelCenter;
    float m_modelSize;
    
    // 动画定时器
    QTimer *m_animationTimer;
    bool m_animationEnabled;
    
    // 视图切换保护机制成员变量
    QTime m_lastViewSwitch;
    bool m_viewSwitchInProgress;
};

#endif // MODELPREVIEW_H
